<?php

namespace app\api\controller\msg;

use app\api\controller\Common;
use GatewayWorker\Lib\Gateway;
use app\api\model\msg\Msgmodel;
use think\facade\Env as getEnv;

class Server extends Common
{
    protected $data;
    public function initialize()
    {
        $this->checkUserLogin(); // 验证登录 TODO 这里需要开启登录验证
        $this->data = $this->request->except(['sign','accessKey','token']);
    }
    /**
     * 检测用户是否在线
     */
    public function checkOnline(){
        $uid = $this->data['uid'];
        $res = Gateway::isUidOnline($uid);
        if(!$res){
            $this->return = ['status'=>200,'code'=>'ONLINE_ERROR','message'=>'离线','data'=>[
                'type' => 'online',
                'res' => 0
            ]];
        }else{
            $this->return = ['status'=>200,'code'=>'SUCCESS','message'=>'成功','data'=>[
                'type' => 'online',
                'res' => 1
            ]];
        }
        return $this->show($this->return);
    }
    /**
     * 绑定client_id与uid
     */
    public function bindUid(){
        $client_id = $this->data['client_id'];
        $uid = $this->data['uid'];
        if($client_id && $uid){
            if(!Gateway::isUidOnline($uid)){
                Gateway::bindUid($client_id,$uid);
                $this->return = ['status'=>200,'code'=>'SUCCESS','message'=>'成功','data'=>[
                    'type' => 'bind',
                    'uid' => $uid,
                    'client_id' => $client_id
                ]];
            }else{
                $this->return = ['status'=>200,'code'=>'BIND_REPEAT_ERROR','message'=>'已经连上了','data'=>''];
            }
        }else{
            $this->return = ['status'=>200,'code'=>'BIND_ERROR','message'=>'绑定失败','data'=>''];
        }
        $send_res = $this->arrayToJson($this->return);
        Gateway::sendToUid($uid,$send_res);
    }

    /**
     * 检测是否有未接收的消息
     */
    public function checkSendMsg(){
        $uid = $this->data['uid'];
        $model = new Msgmodel();
        $send_list = $model->getNoSendMsg($uid);
        if(false !== $send_list){
            foreach ($send_list as $key=>$value){
                $data = ['status'=>200,'code'=>'SUCCESS','message'=>'成功','data'=>[
                    'send_uid' => $value['send_uid'],
                    'receive_uid' => $value['receive_uid'],
                    'type' => $value['type'],
                    'msg' => $value['msg']
                ]];
                $send_res = $this->arrayToJson($data);
                Gateway::sendToUid($uid,$send_res);             // 发送消息给用户
                $model->changeSendMsgStatus($value['id']);      // 修改消息发送状态
            }
        }
    }

    /**
     * 接收客户端数据并通知
     */
    public function sendMsg(){
        $send_uid = $this->data['send_uid'];
        $receive_uid = $this->data['receive_uid'];
        $type = $this->data['type'];
        $model = new Msgmodel();
        $gateway_uid = $receive_uid;
        if($type == 'text'){
            $msg = $this->data['msg'];
        }
        if($type == 'img' || $type == 'voice' || $type == 'video'){
            $file = $this->request->file('msg');
            if($file){
                $info = $file->move(getEnv::get('root_path') . '/public/uploads/msg');
                if($info){
                    $this->data['msg'] = '/uploads/msg/' .$info->getSaveName();
                }else{
                    $this->data['msg'] = '';
                }
            }
            $msg = $this->data['msg'];
        }
        $online = Gateway::isUidOnline($gateway_uid);   // 验证接收消息用户是否在线
        if(!$online){
            $this->data['status'] = 2;
        }else{
            $this->data['status'] = 1;
        }
        $res = $model->sendMsg($this->data);
        if(false !== $res){
            $this->return = ['status'=>200,'code'=>'SUCCESS','message'=>'成功','data'=>[
                'send_uid' => $send_uid,
                'receive_uid' => $receive_uid,
                'type' => $type,
                'msg' => $msg
            ]];
        }else{
            $this->return = ['status'=>200,'code'=>'SEND_ERROR','message'=>'发送失败','data'=>''];
            $gateway_uid = $send_uid;// 消息发送失败时，推送给send_uid
        }
        $send_res = $this->arrayToJson($this->return);
        Gateway::sendToUid($gateway_uid,$send_res); // 发送消息给用户
    }

}